package javaLearning.IO集合;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 比较类的使用
 *
 * @author : K k
 * @date : 10:31 2020/9/22
 */
public class CompareDemo implements Comparable<CompareDemo> {
    private int age;
    public CompareDemo(int age){
        this.age=age;
    }

    @Override
    public int compareTo(CompareDemo o) {
        return o.age-this.age;
    }

    public static void main(String[] args) {
        List<CompareDemo> list=new ArrayList<>();

        CompareDemo c1=new CompareDemo(0);
        CompareDemo c2=new CompareDemo(-2);
        CompareDemo c3=new CompareDemo(3);
        CompareDemo c4=new CompareDemo(5);
        CompareDemo c5=new CompareDemo(10);
        CompareDemo c6=new CompareDemo(-7);
        list.add(c1);
        list.add(c2);
        list.add(c3);
        list.add(c4);
        list.add(c5);
        list.add(c6);

        Collections.sort(list, new Comparator<CompareDemo>() {
            //o2-o1是降序
            @Override
            public int compare(CompareDemo o1, CompareDemo o2) {
                return o2.age-o1.age;
            }
        });
        for (CompareDemo d :
                list) {
            System.out.print(d.age+" ");
        }

    }
}
